package com.dependencies.springdataredis;

import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author zhizhou   2024/8/17 12:01
 */
@Service
public class RedisService {
    
    private final RedisTemplate<String, Object> redisTemplate;
    private final HashOperations<String, String, Object> hashOperations;
    private final ListOperations<String, Object> listOperations;
    private final ValueOperations<String, Object> valueOperations;
    private final SetOperations<String, Object> setOperations;
    private final ZSetOperations<String, Object> zSetOperations;
    private final HyperLogLogOperations<String, Object> hyperLogLogOperations;
    
    public RedisService(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
        this.valueOperations = redisTemplate.opsForValue();
        this.hashOperations = redisTemplate.opsForHash();
        this.listOperations = redisTemplate.opsForList();
        this.setOperations = redisTemplate.opsForSet();
        this.zSetOperations = redisTemplate.opsForZSet();
        this.hyperLogLogOperations = redisTemplate.opsForHyperLogLog();
    }
    
    public void saveValue(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }
    
    public String getValue(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }
    
    //hash
    
    // 保存哈希数据
    public void saveToHash(String key, String field, String value) {
        hashOperations.put(key, field, value);
    }
    
    // 从哈希中获取数据
    public String getFromHash(String key, String field) {
        return (String) hashOperations.get(key, field);
    }
    
    // 删除哈希中的某个字段
    public void deleteFromHash(String key, String field) {
        hashOperations.delete(key, field);
    }
    
    // 获取哈希中的所有键值对
    public Map<String, Object> getAllFromHash(String key) {
        return hashOperations.entries(key);
    }
    
    //list
    
    // 向列表左侧推入值
    public void pushToListLeft(String key, String value) {
        listOperations.leftPush(key, value);
    }
    
    // 向列表右侧推入值
    public void pushToListRight(String key, String value) {
        listOperations.rightPush(key, value);
    }
    
    // 从列表左侧弹出值
    public String popFromListLeft(String key) {
        return (String) listOperations.leftPop(key);
    }
    
    // 从列表右侧弹出值
    public String popFromListRight(String key) {
        return (String) listOperations.rightPop(key);
    }
    
    // 获取列表中所有值
    public List<Object> getList(String key) {
        return listOperations.range(key, 0, -1);
    }
    
    
    //Set
    
    // Set操作
    public void addToSet(String key, String value) {
        setOperations.add(key, value);
    }
    
    public Set<Object> getSetMembers(String key) {
        return setOperations.members(key);
    }
    
    public boolean isMemberOfSet(String key, String value) {
        return setOperations.isMember(key, value);
    }
    
    public void removeFromSet(String key, String value) {
        setOperations.remove(key, value);
    }
    
    // Sorted Set操作
    public void addToSortedSet(String key, String value, double score) {
        zSetOperations.add(key, value, score);
    }
    
    public Set<Object> getSortedSetRange(String key, long start, long end) {
        return zSetOperations.range(key, start, end);
    }
    
    public void removeFromSortedSet(String key, String value) {
        zSetOperations.remove(key, value);
    }
    
    // HyperLogLog操作
    public void addToHyperLogLog(String key, String value) {
        hyperLogLogOperations.add(key, value);
    }
    
    public long countInHyperLogLog(String key) {
        return hyperLogLogOperations.size(key);
    }
    
    // Bitmap操作
    public void setBit(String key, long offset, boolean value) {
        valueOperations.setBit(key, offset, value);
    }
    
    public boolean getBit(String key, long offset) {
        return valueOperations.getBit(key, offset);
    }
}
